package pe.com.cineplanet.controller;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;
import org.springframework.web.servlet.mvc.multiaction.ParameterMethodNameResolver;

import pe.com.cineplanet.bean.Articulo;
import pe.com.cineplanet.bean.Rubro;
import pe.com.cineplanet.cons.Const;
import pe.com.cineplanet.form.ArticuloForm;
import pe.com.cineplanet.service.Service;

public class ArticuloController extends MultiActionController {

	Service service;
	ArticuloForm form;
	
	private Class<?> commandClass;  
	
	public ArticuloController()
    {
        ParameterMethodNameResolver resolver = new ParameterMethodNameResolver();
        resolver.setParamName("method");
        resolver.setDefaultMethodName("buscar");
        setMethodNameResolver(resolver);
    }
	
	public ModelAndView buscar(HttpServletRequest request, HttpServletResponse response)
    throws Exception {
		ModelAndView model = new ModelAndView("articulo/buscar");
		
		form = getForm(request);
		if(form!=null){
			String searchBy = form.getSearchBy();
			String value = form.getValue();
			String message = null;
			
			if(searchBy!=null){
	
				List<Articulo> list = null;
				if(searchBy.equals(Const.SearchBy.DESCRIPTION)){
					//list =  service.listArticulosByName(value);
					list =  new ArrayList<Articulo>();
					list.add(new Articulo("00001", "Medias", 5.0, new Rubro("AN", "Ropa interior")));
					list.add(new Articulo("00002", "Calzoncillo", 12.0, new Rubro("AN", "Ropa interior")));
					list.add(new Articulo("00003", "Short", 25.0, new Rubro("AS", "Ropa Sport")));
					list.add(new Articulo("00004", "Polo", 50.0, new Rubro("AS", "Ropa Sport")));
				}
				if(list!=null && list.size()==0) list =null;
	
				if(list==null){
					message = "nofound";
				}
				model.addObject("msg", message);
				model.addObject("listArticulo", list);
			}

		}
		
		return model;

    } 
	public ModelAndView listar(HttpServletRequest request, HttpServletResponse response)
    throws Exception {

		ModelAndView model = new ModelAndView("articulo/listar");
		
		List<Articulo> list =  new ArrayList<Articulo>();
		list.add(new Articulo("00001", "Medias", 5.0, new Rubro("AN", "Ropa interior")));
		list.add(new Articulo("00002", "Calzoncillo", 12.0, new Rubro("AN", "Ropa interior")));
		list.add(new Articulo("00003", "Short", 25.0, new Rubro("AS", "Ropa Sport")));
		list.add(new Articulo("00004", "Polo", 50.0, new Rubro("AS", "Ropa Sport")));
		model.addObject("listArticulo", list);
		
		
		return model;

    } 
	
	public ModelAndView buscarByRubro(HttpServletRequest request, HttpServletResponse response)
    throws Exception {

		ModelAndView model = new ModelAndView("articulo/buscar/rubro");
		List<Articulo> list = new ArrayList<Articulo>();
		list.add(new Articulo("001","Palo de Lluvia de 70 cms", 10.0, new Rubro("AN", "Rubro1")));
		list.add(new Articulo("001","Palo de Lluvia de 70 cms", 10.0, new Rubro("AN", "Rubro1")));
		list.add(new Articulo("001","Palo de Lluvia de 70 cms", 10.0, new Rubro("AN", "Rubro1")));
		list.add(new Articulo("001","Palo de Lluvia de 70 cms", 10.0, new Rubro("AN", "Rubro1")));
		list.add(new Articulo("001","Palo de Lluvia de 70 cms", 10.0, new Rubro("AN", "Rubro1")));
		list.add(new Articulo("001","Palo de Lluvia de 70 cms", 10.0, new Rubro("AN", "Rubro1")));
		list.add(new Articulo("001","Palo de Lluvia de 70 cms", 10.0, new Rubro("AN", "Rubro1")));
		
		model.addObject("listArticulo", list);

		return model;

    } 
	
	public ModelAndView vista(HttpServletRequest request, HttpServletResponse response)
    throws Exception {

		ModelAndView model = new ModelAndView("articulo/vista");
		String articuloId = request.getParameter("articuloId");
		if(articuloId==null) articuloId = (String)request.getAttribute("articuloId");

		model.addObject("articuloId", articuloId);
		return model;
    } 
	
	public ModelAndView nuevo(HttpServletRequest request, HttpServletResponse response)
    throws Exception {
    	
		ModelAndView model = new ModelAndView("articulo/nuevo");
		//List<Rubro> rubros = service.listRubro();
		List<Rubro> rubros = new ArrayList<Rubro>();
		rubros.add(new Rubro("AN", "Ropa interior"));
		rubros.add(new Rubro("AS", "Ropa Sport"));
		model.addObject("listRubros", rubros);
		return model;

    } 
	
	public ModelAndView vistaData(HttpServletRequest request, HttpServletResponse response)
    throws Exception {
    	
		ModelAndView model = new ModelAndView("articulo/vista/info");
		String articuloId = request.getParameter("articuloId");
		
		//Articulo articulo =  service.getArticulo(articuloId);
		Articulo articulo =  new Articulo("00001", "Medias", 5.0, new Rubro("AN", "Ropa interior"));

		model.addObject("articulo", articulo);
		
		String tipo = request.getParameter("tipo");

		if(tipo==null || tipo.length()==0) tipo = "info";
		if(tipo.equals("editar")){
			//List<Rubro> rubros = service.listRubro();
			List<Rubro> rubros = new ArrayList<Rubro>();
			rubros.add(new Rubro("AN", "Ropa interior"));
			rubros.add(new Rubro("AS", "Ropa Sport"));
			model.addObject("listRubros", rubros);
		}
		
		model.addObject("tipo", tipo);
		
		return model;

    } 

	public ModelAndView save(HttpServletRequest request, HttpServletResponse response)
    throws Exception {

		form = getForm(request);
		if(form!=null){
			String codrubro = form.getCodRubro();
			String descrip = form.getTxtDescripcion();
			Double precio = form.getTxtPrecioVenta();
			Double costou = form.getTxtCostoUnitario();
			
			Articulo articulo = new Articulo(
					descrip,
					precio,
					new Rubro(codrubro)
					);
			
			
			
			request.setAttribute("articuloId", "1000");
			return vista(request, response);
			
			
			//service.save(articulo, true);
			

		}else{
			
			return nuevo(request, response);
			
		}
		

    } 
	
	public void saveEdit(HttpServletRequest request, HttpServletResponse response)
    throws Exception {
    	
		form = getForm(request);
		if(form!=null){
			String articuloId = form.getArticuloId();
			String codrubro = form.getCodRubro();
			String descrip = form.getTxtDescripcion();
			Double precio = form.getTxtPrecioVenta();
			Double costou = form.getTxtCostoUnitario();

			Articulo articulo = new Articulo(
					articuloId,
					descrip,
					precio,
					new Rubro(codrubro)
					);
			
			
			//service.save(articulo, true);
			

		}
		

    } 
	
	
	public ModelAndView delete(HttpServletRequest request, HttpServletResponse response)
    throws Exception {

		form = getForm(request);
		if(form!=null){
			String articuloId = form.getArticuloId();
			//service.delete(new Articulo(articuloId));
		}
		
		return buscar(request, response);

    } 
	
	public ModelAndView stocks(HttpServletRequest request, HttpServletResponse response)
    throws Exception {
		ModelAndView model = new ModelAndView("articulo/stocks");
		form = getForm(request);
		if(form!=null){
			String articuloId = form.getArticuloId();
			model.addObject("articulo", new Articulo("001","Palo de Lluvia de 70 cms", 10.0, new Rubro("AN", "Rubro1")));
		}
		
		return model;

    } 
	
	public ModelAndView kardex(HttpServletRequest request, HttpServletResponse response)
    throws Exception {

		ModelAndView model = new ModelAndView("articulo/kardex");
		form = getForm(request);
		if(form!=null){
			String articuloId = form.getArticuloId();
			model.addObject("articulo", new Articulo("001","Palo de Lluvia de 70 cms", 10.0, new Rubro("AN", "Rubro1")));
		}
		return model;

    } 
	
	public ModelAndView movimientos(HttpServletRequest request, HttpServletResponse response)
    throws Exception {
    	
		ModelAndView model = new ModelAndView("articulo/movimientos");
		
		return model;

    } 
	
	public ModelAndView movimientosVentas(HttpServletRequest request, HttpServletResponse response)
    throws Exception {
    	
		ModelAndView model = new ModelAndView("articulo/movimientos/ventas");
		
		return model;

    } 
	
	public Object getCommandObject(HttpServletRequest request)
	throws Exception {
		Object command = formBackingObject(request);
		bind (request, command);
		return command;
	}
	
	protected Object formBackingObject(HttpServletRequest request)
	throws Exception {
		return commandClass.newInstance();
	}

	public Class<?> getCommandClass() {
		return commandClass;
	}

	public void setCommandClass(Class<?> commandClass) {
		this.commandClass = commandClass;
	}
	
	public ArticuloForm getForm(HttpServletRequest request) {
		try {
			Object command = getCommandObject(request);
			form = (ArticuloForm)command;
			return form;
		} catch (Exception e) {
			return null;
		}
    	
	}
	
	public void setService(Service service) {
		this.service = service;
	}
	
	public Service getService() {
		return service;
	}
	

}
